package com.voto.sunflower.activity.me;

import java.util.Hashtable;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.voto.sunflower.R;
import com.voto.sunflower.SunflowerApplication;
import com.voto.sunflower.dao.User;
import com.voto.sunflower.utils.ExUtils;
import com.voto.sunflower.utils.URLHelper;

public class MyQRCodeActivity extends Activity {

	private String AVATAR_TEMP_PATH = SunflowerApplication.AVATAR_TEMP_PATH
			+SunflowerApplication.getmUser().getId()
			+SunflowerApplication.AVATAR_NAME;
	private final static String TAG = "MyQRCodeActivity";
	private int QR_WIDTH = 500;
	private int QR_HEIGHT = 500;
	private ImageView qrImageView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_me_qr_code_detail);

		initUnit();
		initView();

	}

	@Override
	protected void onResume() {
		super.onResume();
		setUser();
	}

	private void initUnit() {
		// 单位转换
		DisplayMetrics dm = getResources().getDisplayMetrics();
		int paddingValue = (int) dm.density;
		// 设置尺寸
		QR_WIDTH = QR_HEIGHT = 240 * paddingValue;
	}

	private void setUser() {
		User user = SunflowerApplication.getmUser();
		if (user != null) {
			String avatar_uri = user.getAvatar_url();
			String QrStr = user.getPhone();
			String name = user.getName();
			String role = user.getRole();
			
			ImageView avatarImageView = (ImageView) findViewById(R.id.avatar_img_qr_code);
			TextView roleTextView = (TextView) findViewById(R.id.parents_info_situation);
			TextView nameTextView = (TextView) findViewById(R.id.parents_info_name);

			if (!TextUtils.isEmpty(QrStr)) {
				createQRImage(QrStr);
			}
			setMyAvatar(avatar_uri,avatarImageView);
			nameTextView.setText(name);
			// 设置角色
			if (!TextUtils.isEmpty(role) && role.equals("teacher")) {
				roleTextView.setText("老师");
			} else if (!TextUtils.isEmpty(role) && role.equals("parent")) {
				roleTextView.setText("家长");
			} else {
				roleTextView.setText("学生");
			}

		}
	}
	
	/**
	 * 先从本地获取，在从网络获取
	 * @param avatar_uri
	 * @param avatarImageView
	 */
	private void setMyAvatar(String avatar_uri,ImageView avatarImageView) {
		// 获取头像，有就缓存
		Bitmap bitmap = BitmapFactory
				.decodeFile(AVATAR_TEMP_PATH);
		if (bitmap != null) {
			avatarImageView.setImageBitmap(bitmap);
		} else {
			ExUtils.loadImage(URLHelper.getAbsoluteUrl(avatar_uri),
					R.drawable.unkown_person, avatarImageView);
		}
	}

	// 初始化标题栏
	private void initView() {
		TextView actionBarTitle = (TextView) findViewById(R.id.title);
		TextView actionBarBack = (TextView) findViewById(R.id.back);
		qrImageView = (ImageView) findViewById(R.id.qr_code_pic);

		actionBarTitle.setText(getString(R.string.me_main_my_qr_code));
		actionBarBack.setText(getString(R.string.me_main_title));
		actionBarBack.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}

	// 生成QR图
	public void createQRImage(String qrStr) {
		try {
			// 需要引入core包
			QRCodeWriter writer = new QRCodeWriter();

			if (qrStr == null || "".equals(qrStr) || qrStr.length() < 1) {
				return;
			}
			// 把输入的文本转为二维码
			BitMatrix martix = writer.encode(qrStr, BarcodeFormat.QR_CODE, QR_WIDTH, QR_HEIGHT);

			System.out.println("w:" + martix.getWidth() + "h:" + martix.getHeight());

			Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
			hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
			BitMatrix bitMatrix = new QRCodeWriter().encode(qrStr, BarcodeFormat.QR_CODE, QR_WIDTH, QR_HEIGHT, hints);
			int[] pixels = new int[QR_WIDTH * QR_HEIGHT];
			for (int y = 0; y < QR_HEIGHT; y++) {
				for (int x = 0; x < QR_WIDTH; x++) {
					if (bitMatrix.get(x, y)) {
						pixels[y * QR_WIDTH + x] = 0xff000000;
					} else {
						pixels[y * QR_WIDTH + x] = 0xffffffff;
					}

				}
			}

			Bitmap bitmap = Bitmap.createBitmap(QR_WIDTH, QR_HEIGHT, Bitmap.Config.ARGB_8888);

			bitmap.setPixels(pixels, 0, QR_WIDTH, 0, 0, QR_WIDTH, QR_HEIGHT);
			qrImageView.setImageBitmap(bitmap);

		} catch (WriterException e) {
			e.printStackTrace();
		}
	}
}
